/* 
 * Copyright (C) 2009 Jeffrey Rusterholz
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// This source was created with the example at:
// http://www.anddev.org/parsing_xml_from_the_net_-_using_the_saxparser-t353.html

package nl.jalava.acalleto;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class CalendarsHandler extends DefaultHandler {
    // Fields
    private boolean titleTag = false; // For the <title> tag.
    private boolean entryTag = false; // <entry> tag.
    
    private ParsedCalendarsDataSet myParsedCalendarsDataSet = new ParsedCalendarsDataSet();

    // Getter & Setter
    public ParsedCalendarsDataSet getParsedData() {
         return this.myParsedCalendarsDataSet;
    }

    @Override
    public void startDocument() throws SAXException {
         this.myParsedCalendarsDataSet = new ParsedCalendarsDataSet();
    }

    @Override
    public void endDocument() throws SAXException {
         // Nothing to do
    }

    /** Gets be called on opening tags like:
     * <tag>
     * Can provide attribute(s), when xml was like:
     * <tag attribute="attributeValue">*/
    @Override
    public void startElement(String namespaceURI, String localName,
              String qName, Attributes atts) throws SAXException {
         if (localName.equals("title")) {
              this.titleTag = true;
         }
         else if (localName.equals("entry")) {
        	 this.entryTag = true;
         }
    	
    }
    
	    /** Gets be called on closing tags like:
	     * </tag> */
	    @Override
	    public void endElement(String namespaceURI, String localName, String qName)
	              throws SAXException {
	         if (localName.equals("title")) {
	              this.titleTag = false;
	         }
	         else if (localName.equals("entry")) {
	             this.entryTag = false;
	        }
	    }
    
		/** Gets be called on the following structure:
		 * <tag>characters</tag> */
		@Override
		public void characters(char ch[], int start, int length) {
		     if(this.entryTag  && this.titleTag){
		    	 myParsedCalendarsDataSet.setExtractedString(new String(ch, start, length));
		}
   } 
}
